# AdaBoost实战葡萄酒数据
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import AdaBoostClassifier  # 集成学习
from sklearn.metrics import accuracy_score

# 获取数据
df = pd.read_csv('wine0501.csv')

#数据处理
#把类别中的Class label ：3过滤掉
df = df[df['Class label'] != 3]
# print(df)
#获取特征列 : 'Alcohol','Hue' ; 标签列：'Class label'
x = df[['Alcohol', 'Hue']]
y = df['Class label']

# 通过标签编码器 把标签列 转换为数值列   Series[1 2 ]    ndarray[0 1 ]
encoder = LabelEncoder()
y = encoder.fit_transform(y)  # 0,1

#拆分训练集和测试集
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=20)
#特征工程  此处暂时省略

#单一决策树
#创建模型对象 : DecisionTreeClassifier
model1 = DecisionTreeClassifier(max_depth=5)
#训练模型
model1.fit(x_train, y_train)
#预测
y_pre =model1.predict(x_test)

#模型评估: accuracy_score
print('准确率：', accuracy_score(y_test, y_pre))

# 场景2  -》集成学习   CART树 : AdaBoostClassifier
'''
AdaBoostClassifier
estimator = es
n_estimators = x
learning_rate = x 
algorithm='SAMME'
'''
model_cls = DecisionTreeClassifier(max_depth=5)
model2 = AdaBoostClassifier(estimator=model_cls, n_estimators=5, algorithm='SAMME.R')
model3 = AdaBoostClassifier(algorithm='SAMME')

# 模型训练
model2.fit(x_train, y_train)
model3.fit(x_train, y_train)
# 预测
y_pre_2 = model2.predict(x_test)
y_pre_3 = model3.predict(x_test)
# 预测正确率：accuracy_score
print('准确率：', accuracy_score(y_test, y_pre_2))
print('准确率：', accuracy_score(y_test, y_pre_3))